package com.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.item.mapper.EmpMapper;
import com.item.pojo.Emp;
import com.item.service.EmpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Override
    public Map<String, Object> findPage(Integer page, Integer limit, String empName, String phone, String storeName) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Emp> query = new QueryWrapper<Emp>();
        query.orderByDesc("eid");
//        query.orderByAsc("eid");
        if (empName != null && empName != "") {
            query.like("ename", empName);
        }
        if (phone != null && phone != "") {
            query.like("phone", phone);
        }
        if (storeName != null && storeName != "" && !storeName.equals("0")) {
            query.eq("store", storeName);
        }
        Page<Emp> pages = new Page<Emp>(page, limit);
        IPage<Emp> iPage = super.baseMapper.selectPage(pages, query);
        List<Emp> list = iPage.getRecords();
        long count = iPage.getTotal();
        map.put("code", 0);
        map.put("count", count);
        map.put("data", list);
        return map;
    }

    @Override
    public boolean updateEmpState(Integer id, Integer state) {
        return super.baseMapper.updateEmpState(id, state) != 0 ? true : false;
    }

    @Override
    public boolean updateEmpInfo(Emp emp) {
        return super.baseMapper.updateEmpInfo(emp) != 0 ? true : false;
    }
}
